﻿jQuery(document).ready(function () {
    //页面初始化
    //  CommonModalWorkflowSubmit.init();

      CommonModalDocListModal.init();

});

var CommonModalWorkflowSubmit = function () {
    var systemNo = $("#Layout-SystemNo").val();
    var moduleNo = $("#Layout-ModuleNo").val();
    var modalId = "modalWorkflowSubmit";
    var tableListId = "tableWorkflowSubmitOption";
    //nodeInfo:WorkflowNodeInfo
    //submitInit:提交初始信息,WorkflowSubmitInitInfo
    //paras:提交初始信息,WorkflowSubmitInitInfo
    //submitAfter:提交后调用的函数,参数：data：dataSubmitWorkflowInfoDto
    //isSubmit:true
    var setting;
    var dataSubmitWorkflowInfoDto;


    var initTableList = function (dataSet) {
        var tableId = tableListId;

        var option = {
            "pagingType": "bootstrap_full_number",
            "columns": [
                { "data": "RowGuid", "visible": false }, //隐藏checkbox,"visible": false 
                { "data": "RowId" },
                {
                    "data": "Result",
                    render: function (data, type, row) {
                        var res;
                        switch (data) {
                            case 0:
                                res = "提交";
                                break;
                            case 1:
                                res = "提交并上报";
                                break;
                            case 2:
                                res = "驳回";
                                break;
                            case 5:
                                res = "抢单";
                                break;
                            case 6:
                                res = "结束";
                                break;
                            default:
                                res = "";
                        }

                        return res;
                    }
                },
                { "data": "ToNodeName" },
                { "data": "ToNodeNameUserNo" },
            ],
            data: dataSet,
            "ajax": null,

        };
        var table = ComFuncDt.bindDataTable(tableId, option);

    };


    var ajaxFormSubmit = function () {

        //得到form数据
        var newData = $('#' + modalId + '-form').serializeArray();
        $.each(newData, function (i, field) {

            dataSubmitWorkflowInfoDto[field.name] = field.value;

        });

        var data;
        var datas = ComFuncDt.getSelectDatas(tableListId);
        if (datas.length > 0) {
            data = datas[0];
            dataSubmitWorkflowInfoDto["SubmitResult"] = { Result: data.Result, ToNodeName: data.ToNodeName };
            dataSubmitWorkflowInfoDto["NextNodeTransactUserNo"] = data.ToNodeNameUserNo;
        } else {
            ComApp.messageWarning("请先选择一个提交选项");
            return;
        }
        dataSubmitWorkflowInfoDto["InstanceId"] = setting.nodeInfo.InstanceId;
        dataSubmitWorkflowInfoDto["InstanceNodeId"] = setting.nodeInfo.NodeInstanceNodeId;
        dataSubmitWorkflowInfoDto.Paras = setting.paras;

        
        $('#' + modalId).modal('hide');


        $.post('/Base/WorkflowTaskSubmit', dataSubmitWorkflowInfoDto, function (msg) {
            if (msg.Result) {
                if (setting.submitAfter) {
                    if (jQuery.isFunction(setting.submitAfter)) {
                        setting.submitAfter(dataSubmitWorkflowInfoDto);
                    }
                }

            } else {
                $('#' + modalId).modal('show');
                ComApp.bootboxAlert("流程提交失败，" + msg.Message);
            }
        });

        

    };

    var handleValidation = function () {

        var ruls = {
            SubmitDescription: {
            required: true
            }
        };
        ComApp.formValidation(modalId + '-form', ruls, ajaxFormSubmit);


    }

    var workflowSubmitModal = function () {
        var isSubmit = setting.isSubmit;
        var dataWorkflowNodeInfo = setting.nodeInfo;
        if (isSubmit) {
            $("textarea[name='SubmitDescription']").val("提交");
        }

        var dataset = new Array();
        //提交选项
        var nodeSubmitOption = JSON.parse(dataWorkflowNodeInfo.NodeSubmitOption);
        var isHasOption = false;
        for (var i in nodeSubmitOption) {
            var option = nodeSubmitOption[i];
            option.RowGuid = "";
            option.RowId = "";
            option.ToNodeNameUserNo = "";
            //是提交
            if (isSubmit && (option.Result === 0 || option.Result===1)) {
              dataset.push(option);
                isHasOption = true;
            } else {
                //驳回
                if (option.Result===2) {
                    dataset.push(option);
                    isHasOption = true;
                }
            }
        }
        if (!isHasOption) {
            ComApp.messageWarning("没有你选择的提交选项！");
            return;
        }
        ComFuncDt.initDetailTableData(tableListId, dataset);

        //事件绑定
        if (isSubmit) {
            $('#' + modalId + ' .modal-type-submit').show();
            $('#' + modalId + ' .modal-type-overrule').hide();

        } else {
            $('#' + modalId + ' .modal-type-submit').hide();
            $('#' + modalId + ' .modal-type-overrule').show();
        }

        $('#' + modalId).modal('show');
    };

    var workflowSubmitInit = function () {

        $.post('/Base/WorkflowSubmitInit', setting.nodeInfo, function (msg) {
            if (msg.Result) {
                setting.submitInit = msg.data;
                workflowSubmitModal(setting);
            } else {
                ComApp.bootboxAlert("提取流程提交初始信息失败，" + msg.Message);
            }
        });


    }


    var handleInit = function() {
        dataSubmitWorkflowInfoDto = {};
        dataSubmitWorkflowInfoDto.BusinessData = "";
        dataSubmitWorkflowInfoDto.BusinessGroupId = "";
        dataSubmitWorkflowInfoDto.DynamicList = null;
        dataSubmitWorkflowInfoDto.ParallelList = null;

        dataSubmitWorkflowInfoDto.InstanceId = "";
        dataSubmitWorkflowInfoDto.InstanceNodeId = "";
        dataSubmitWorkflowInfoDto.SubmitUserNo = "";
        dataSubmitWorkflowInfoDto.SubmitUserName = "";
        dataSubmitWorkflowInfoDto.SubmitResult = null;
        dataSubmitWorkflowInfoDto.SubmitDescription = "";
        dataSubmitWorkflowInfoDto.Paras = null;
        dataSubmitWorkflowInfoDto.NextNodeTransactUserNo = "";
        dataSubmitWorkflowInfoDto.NextNodeTransactUserName = "";
    };
    return {
        init: function () {
            handleInit();
            //事件绑定
            initTableList();
            handleValidation();

        },
        openWorkflowSubmitModal: function (set) {
            setting = set;
            workflowSubmitInit();
        },
    };
}();

var CommonModalDocListModal = function () {
    var systemNo = $("#Layout-SystemNo").val();
    var moduleNo = $("#Layout-ModuleNo").val();
    var modalId = "modalDocListModal";
    var tableListId = "tableDocList";
    var isInitTable = false;
    var setting;//GroupGuid:关联文档表的Id


    var initTableList = function () {
        var tableId = tableListId;
        //如果已经初始化，则直接重新加载数据
        if (isInitTable) {
            ComFuncDt.reload(tableId);
            return;
        } 
        //dataTables的选项
        var option = {
            "dom": "fr<'table-scrollable't>i",
            "pageLength": 5, // 每页显示记录数
            "columns": [
                { "data": "RowGuid", "visible": false }, //隐藏checkbox,"visible": false 
                { "data": "RowId" },
                { "data": "Name" },
                { "data": "ModifyUserNo" },
                { "data": "ModifyTime", render: ComFuncDt.dataTableColRender("DateTime") },
            ],
            "ajax": {
                url: "/Base/QueryEcFiles",
                data: function (data) {
                    data.Key1 = setting.GroupGuid;
                    return JSON.stringify(data);
                }
            }
        };
        var table = ComFuncDt.bindDataTable(tableId, option);
        isInitTable = true;

        $('#' + tableId).on('click', 'tbody tr .openModelEditRow', function () {
            var data = table.api().row($(this).parents('tr')).data();

//            openModelWorkflowLogDetail(data);

        });

    };


    var handleInit = function() {



    };

    var openDocListModal = function (set, isEdit) {
        setting = set;
        if (isEdit) {
            $('#' + modalId + " #btnUpload").show();
            $('#' + modalId + " #btnDel").show();
        } else {
            $('#' + modalId + " #btnUpload").hide();
            $('#' + modalId + " #btnDel").hide();
        }

        var url = '/Base/UploadEcFiles?groupGuid=' + setting.GroupGuid;
        var uploadId = 'fileEcFile';

        var fileInput = ComFuncFileInput.initFileInput(uploadId, url);

        fileInput.on("fileuploaded", function (event, data) {
            if (data.response) {
                ComApp.messageSuccess(JSON.stringify(data.success));
            }
        });

        $('#' + modalId + " #btnUpload").click(function () {
            $("#" + uploadId).fileinput("upload");
        });


        $('#' + modalId).modal('show');
        initTableList();
    };
    return {
        init: function () {
            handleInit();
            //

        },
        openDocListModal: function (set) {
            openDocListModal(set);
        },
        //rbacwf对话框的id
        setWfFileGroupClick:function(id) {
            $("#" + id + " .rbacwf-CtrlType-FileGroup").click(function () {
                var name = this.getAttribute("data-filegroup");
                var edit = this.getAttribute("data-filegroup-edit");
                var guid = $("#" + id + " input[name='" + name + "']").val();
                var set = { GroupGuid: guid };
                if ((edit) && edit === 'True') {
                    openDocListModal(set,true);
                } else {
                    openDocListModal(set);
                }
            });
        }
    };
}();

